Ext.onReady(function(){
	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
		dock : 'top',
		items : [ {
			text : '添加',
			xtype : 'button',
			listeners:{
				click: function(){
					location.href = "sysMgr/add_msg";
				}
			}
		},'-', {
			text : '删除',
			xtype : 'button',
			listeners:{
				click: function(){
					var grid = Ext.getCmp("msg_grid");
					var selectionModel = grid.getSelectionModel();
					var models = selectionModel.getSelection();
					var ids = [];
					for(var i=0; i<models.length;i++){
						ids[i] = models[i].get("Id");
					}
					Ext.Ajax.request({
						url: '/sms_task/delete',
						type: 'POST',
						params:{
							ids: ids
						},
						callback:function(options,success, response){
							if(success){
								var result  = Ext.decode(response.responseText);
								if(result.success){
									Ext.getCmp("msg_grid").getStore().reload();
								}
							}
						}
					});
				}
			}
		},{
			text: '刷新',
			xtype: 'button',
			listeners:{
				click: function(){
					Ext.getCmp("msg_grid").getStore().reload();
				}
			}
		}]
	});
	var store = Ext.create('Ext.data.Store', {
	    fields:['Id', 'NotifyContent', 'NotifyTime', 'NotifyTimes',{
	    	name: "Msgs", type: "object"
	    }],
	    autoLoad:true,
	    pageSize: 25,
	    proxy: {
	        type: 'ajax',
	        url: '/sms_task/all',
	        reader: {
	            type: 'json',
	            root: 'data.rows',
	            totalProperty: 'data.total'
	        }
	    }
	});
	var grid = Ext.create('Ext.grid.Panel', {
		id: 'msg_grid',
		margin: '5 5',
		store: store,
		selType : 'checkboxmodel',
		columns : [ {
			text : '序号',
			xtype : 'rownumberer',
			width : 40,
			sortable : false
		}, {
			text : '短信内容',
			flex : 1,
			dataIndex : 'NotifyContent'
		}, {
			text : '短信对象',
			dataIndex : 'Msgs',
			flex:1,
			renderer: function(v){
				var users = "";
				if(!Ext.isEmpty(v)){
					for(var i=0; i<v.length; i++){
						users += v[i].User.Username + '(' + v[i].User.Name + ') ';
					}
				}
				return users;
			}
		},{
			text: "提醒时间",
			width : 200,
			dataIndex: "NotifyTime"
		},{
			text: "提醒周期",
			width : 200,
			dataIndex: "NotifyTimes",
			renderer: function(v){
				if(v==0){
					return '立刻';
				}
				else{
					return v + '小时';
				}
			}
		},{
			text: "详细",
			width:60,
			renderer: function(v, meta, record){
				return '<a href="sms_task/detail/' + record.get('Id') + '">详细</a>';
			}
		}],
		bbar : Ext.create('Ext.PagingToolbar', {
							store : store,
							border: false,
							displayInfo : true,
							displayMsg : '显示{0} - {1} / 总数 {2}',
							emptyMsg : "没数据"
						})
	});
	var panel = Ext.create("Ext.panel.Panel", {
		border : false,
		bodyBorder : false,
		layout : 'fit',
		dockedItems : [ toolbar ],
		items : [ grid ]
	});
	setInterval(function(){
		grid.getStore().reload();
	}, 5000);
	Ext.create('Ext.Viewport', {
		layout : 'fit',
		style : 'background:none;background-color:white;',
		items : [ panel ]
	});
});